(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.process.processConfig.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'processConfigService', 'ajaxServiceFactory', '$q' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, processConfigService, ajaxServiceFactory, $q) {
		var ctrl = this, processConfigInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.processConfig = {};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.variablesCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.processConfig.variables';
			authService.create().then(function(response) {
				return $q(function(resolved, rejected) {
					resolved(response);
				});
			}).then(function(response) {
				var parm = {};
				var tmpProcessConfig = angular.merge(response.data, processConfigInfo) || processConfigInfo;
				parm.serviceCode = tmpProcessConfig.serviceCode;
				processConfigService.getBpmprocContextOptions(parm).then(function(resOption) {
					
					ctrl.bpmprocContextOptions = resOption;
					ctrl.bpmprocContextOptions.hasEmptyOption = true;
					ctrl.processConfig = angular.merge(response.data, processConfigInfo) || processConfigInfo;
					ctrl.processConfig.variables = ctrl.processConfig.variables || [];
					ctrl.processConfig.process = {};
					angular.forEach(processConfigInfo, function(value, key) {
						ctrl.processConfig.process[key] = value;
					});
					// reset ProcessVariableModel key for create
					if (ctrl.processConfig.variables.length == 0) {
						var wfprocVarArray = processConfigInfo.wfprocVars.split(",");
						for (var i = 0; i < wfprocVarArray.length; i++) {
							var variable = {};
							variable.key = wfprocVarArray[i];
							ctrl.processConfig.variables.push(variable);
						}
					}
				});
			});
		};
		
		ctrl.companyCodeChanged = function() {
			ctrl.processConfig.companyCode = ctrl.processConfig.companyCode;
			if ( !ctrl.processConfig.companyCode ) {
				ctrl.processConfig.companyCode = '*';
				ctrl.processConfig.companyName = '*';
			}
		};
		
		ctrl.companyCpCodeChanged = function() {
			ctrl.processConfig.companyCpCode = ctrl.processConfig.companyCpCode;
			if ( !ctrl.processConfig.companyCpCode ) {
				ctrl.processConfig.companyCpCode = '*';
				ctrl.processConfig.companyCpName = '*';
			}
		};
		
		ctrl.createSave = function() {
			if (ctrl.processConfig.taskId) {
				return authService.createContinueSave(ctrl.processConfig).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.processConfig).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			if (ctrl.processConfig.taskId) {
				return authService.createContinueSubmit(ctrl.processConfig).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.processConfig).then(submitSuccessful, submitFailed);
			}
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedVariables = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.process.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.process.list',
					'state' : 'cda.process.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
